package com.example.hospitalManage.controller.admin.api;

import com.example.hospitalManage.common.Constant;
import com.example.hospitalManage.common.ServiceResultEnum;
import com.example.hospitalManage.config.annotation.TokenToAdminUser;
import com.example.hospitalManage.controller.admin.param.AdminLoginParam;
import com.example.hospitalManage.controller.admin.param.UpdateAdminNameParam;
import com.example.hospitalManage.controller.admin.param.UpdateAdminPasswordParam;
import com.example.hospitalManage.entity.AdminUser;
import com.example.hospitalManage.entity.AdminUserToken;
import com.example.hospitalManage.service.AdminUserService;
import com.example.hospitalManage.utils.Response;
import com.example.hospitalManage.utils.ResponseResult;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @ProjectName: yygh
 * @Author: gxy
 * @Time: 2022/12/30 0:02
 * @Description: 描述
 */
@RestController
@RequestMapping(value = "/api")
@Api(value = "v1",tags = "管理端用户请求相关接口")
@CrossOrigin
public class AdminUserManageApi {

    @Resource
    private AdminUserService adminUserService;

    private static final Logger logger = LoggerFactory.getLogger(AdminUserManageApi.class);



    @RequestMapping(value = "/adminUser/login", method = RequestMethod.POST)
    public ResponseResult<String> login(@RequestBody @Valid AdminLoginParam adminLoginParam) {
        String loginResult = adminUserService.login(adminLoginParam.getUserName(), adminLoginParam.getPasswordMd5());
        logger.info("manage login api,adminName={},loginResult={}", adminLoginParam.getUserName(), loginResult);

        //登录成功
        if (!StringUtils.isEmpty(loginResult) && loginResult.length() == Constant.TOKEN_LENGTH) {
            return Response.createOkResp("登陆成功",loginResult);
        }
        //登录失败
        return Response.createFailResp(loginResult);
    }

    @RequestMapping(value = "/adminUser/profile", method = RequestMethod.GET)
    public ResponseResult profile(@TokenToAdminUser AdminUserToken adminUser) {
        logger.info("adminUser:{}", adminUser.toString());
        AdminUser adminUserEntity = adminUserService.getUserDetailById(adminUser.getAdminUserId());
        if (adminUserEntity != null) {
            adminUserEntity.setLoginPassword("******");
            return Response.createOkResp("查询成功",adminUserEntity);
        }
        return Response.createFailResp(ServiceResultEnum.DATA_NOT_EXIST.getResult());
    }

    @RequestMapping(value = "/adminUser/password", method = RequestMethod.PUT)
    public ResponseResult passwordUpdate(@RequestBody @Valid UpdateAdminPasswordParam adminPasswordParam, @TokenToAdminUser AdminUserToken adminUser) {
        logger.info("adminUser:{}", adminUser.toString());
        if (adminUserService.updatePassword(adminUser.getAdminUserId(), adminPasswordParam.getOriginalPassword(), adminPasswordParam.getNewPassword())) {
            return Response.createOkResp();
        } else {
            return Response.createFailResp(ServiceResultEnum.DB_ERROR.getResult());
        }
    }

    @RequestMapping(value = "/adminUser/name", method = RequestMethod.PUT)
    public ResponseResult nameUpdate(@RequestBody @Valid UpdateAdminNameParam adminNameParam, @TokenToAdminUser AdminUserToken adminUser) {
        logger.info("adminUser:{}", adminUser.toString());
        if (adminUserService.updateName(adminUser.getAdminUserId(), adminNameParam.getLoginUserName(), adminNameParam.getNickName())) {
            return Response.createOkResp();
        } else {
            return Response.createFailResp(ServiceResultEnum.DB_ERROR.getResult());
        }
    }

    @RequestMapping(value = "/adminUser/logout", method = RequestMethod.DELETE)
    public ResponseResult logout(@TokenToAdminUser AdminUserToken adminUser) {
        logger.info("adminUser:{}", adminUser.toString());
        adminUserService.logout(adminUser.getAdminUserId());
        return Response.createOkResp();
    }


}
